package GDoc::Job::Fetch;

use Moose;

use Net::Amazon::S3;
use Scalar::Util 'blessed';
use WWW::Mechanize;

use GDoc::AWS '$AWS_ACCESS_KEY_ID', '$AWS_SECRET_KEY';
use GDoc::Logger;

has 'logger'       => ( is        => 'ro',
                        init_arg  => undef,
                        default   => sub { GDoc::Logger->get_logger } 
                      );

has 'data'         => ( is       => 'ro', 
                        required => 1, 
                        isa      => 'HashRef' 
                      );

has 'fetch_queue'  => ( is       => 'ro',
                        init_arg => undef,
                        default  => 'https://queue.amazonaws.com/fetch'
                      );

has 'docs_bucket'  => ( is       => 'ro',
                        init_arg => undef,
                        default  => 'docs.govdoc.org'
                      );

has 's3'           => ( is       => 'ro',
                        init_arg => undef,
                        default  => sub { 
                            Net::Amazon::S3->new( aws_access_key_id => $AWS_ACCESS_KEY_ID,
                                                  aws_secret_access_key => $AWS_SECRET_KEY,
                                                  retry => 1 )
                            }
                      );

has '_baseurl' => ( is       => 'rw' );
has '_basedir' => ( is       => 'rw' );

has '_mech'    => ( is       => 'ro',
                    init_arg => undef,
                    default  => sub { WWW::Mechanize->new }
                  );



# abstract methods (should these be those newfangled "role" things?)
sub setup { 
    return;
}

sub run { 
    my $self = shift;
    die "No run() method defined for " . blessed $self;
}

sub store_pdf { 
    my $self = shift;
    my %args = @_;

    die 'No key specified' unless $args{key};
    die 'No data'          unless $args{data} || $args{datafile};

    my $s3 = $self->s3;

    my %disp = exists( $args{filename} )
      ? ( 'content_disposition' => sprintf( 'attachment; filename="%s"', $args{filename} ) )
      : ( );

    my %meta = exists( $args{meta} )
      ? ( map { 'x-amz-meta-' . $_ => $args{meta}{$_} } keys %{ $args{meta} } )
      : ( );

    my $meth = $args{datafile} ? 'add_key_filename' : 'add_key';

    my $bkt = $s3->bucket( $self->docs_bucket );
    $bkt->$meth( $args{key} => ( $meth eq 'add_key' ? $args{data} : $args{datafile}->filename ), 
                 { content_type => 'application/pdf',
                   acl_short    => 'public-read',
                   %disp,
                   %meta
                 }
               );

}


sub store_xml { 
    my $self = shift;
    my %args = @_;

    die 'No key specified' unless $args{key};
    die 'No data'          unless $args{data};

    my %meta = exists( $args{meta} )
      ? ( map { 'x-amz-meta-' . $_ => $args{meta}{$_} } keys %{ $args{meta} } )
      : ( );

    my $s3 = $self->s3;
    my $bkt = $s3->bucket( $self->docs_bucket );
    $bkt->add_key( $args{key} => $args{data},
                   { content_type => 'text/xml',
                     acl_short    => 'public-read',
                     %meta
                   }
                 );
}


sub store_xls { 
    my $self = shift;
    my %args = @_;

    die 'No key specified' unless $args{key};
    die 'No data'          unless $args{data};

    my %disp = ( 'content_disposition' => sprintf( 'attachment; filename="%s"', $args{filename} ) );

    my %meta = exists( $args{meta} )
      ? ( map { 'x-amz-meta-' . $_ => $args{meta}{$_} } keys %{ $args{meta} } )
      : ( );


    my $s3 = $self->s3;
    my $bkt = $s3->bucket( $self->docs_bucket );
    $bkt->add_key( $args{key} => $args{data},
                   { content_type => 'application/excel',
                     acl_short    => 'public-read',
                     %disp,
                     %meta
                   }
                 );
}



1;
